package gentle.controller;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.BaseController;
import gentle.entity.SearchRechargeInfo;
import gentle.factory.PageFactory;
import gentle.service.IRechargeService;
import gentle.exception.ResubmitLock;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import gentle.entity.RechargeInfo;

import java.util.List;

import gentle.util.ToolUtil;
import gentle.entity.PayServicedInfo;
import gentle.exception.BussinessException;
import gentle.exception.BizExceptionEnum;


/**
 * 结账退款
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/recharge")
public class RechargeController extends BaseController {
    @Autowired
    IRechargeService rechargeService;

    /**
     * 结账退款列表查询
     */
    @RequestMapping("/rechargeList")
    @ResponseBody
    public Object rechargeList(SearchRechargeInfo info) {
        if (info.getAbbreviation() == null || info.getAbbreviation() == "" || info.getAbbreviation() == "null") {
            return null;
        } else {
            Page<RechargeInfo> page = new PageFactory<RechargeInfo>().defaultPage();
            try {
                info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                List<RechargeInfo> list = rechargeService.selectPageListRechargeInfo(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = rechargeService.selectPageListRechargeInfo(page, info);
                }
                page.setTotal(rechargeService.getQueryPagesCount(page, info));
                page.setRecords(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.packForBT(page);
        }
    }

    /**
     * 统计查询
     */
    @RequestMapping(value = "/rechargeTotal", method = RequestMethod.GET)
    @ResponseBody
    public List<RechargeInfo> rechargeTotal(String startArrivalTimeTwo, String endArrivalTimeTwo,
                                            String dealStatus, String oldOneChannelValue, String reach,
                                            String oldTwoChannelValue, String oldThrChannelValue, String abbreviation) throws Exception {
        return rechargeService.rechargeTotal(startArrivalTimeTwo, endArrivalTimeTwo, dealStatus, oldOneChannelValue, reach, oldTwoChannelValue, oldThrChannelValue, abbreviation);
    }

    private final static Object PRESENT = new Object();

    @RequestMapping(value = "/checkOut", method = RequestMethod.POST)
    @ResponseBody
    @Around("@annotation(gentle.exception.ResubmitLock)")
    public String checkOut(String reservationsIdx, String customerId, String reservationsId, String[] str, String payorrefund, String orthodontics, double totleServiceActualPay1,
                           String operationTime, String getTime, String abbreviation, String outpatientName, String areaAbbreviation, String userAccount, String sttleAccountsTime, String proportional) {
        String key = "";
        key = ResubmitLock.handleKey(getTime);
        //执行锁
        boolean lock = false;
        try {
            //设置解锁key
            lock = ResubmitLock.getInstance().lock(key, PRESENT);
            if (lock) {
                //放行
                return rechargeService.checkOut(reservationsIdx, customerId, reservationsId, str, payorrefund, orthodontics, totleServiceActualPay1, operationTime, getTime,
                        abbreviation, outpatientName, areaAbbreviation, userAccount, sttleAccountsTime, proportional);
            } else {
                //响应重复提交异常
                return "响应重复提交异常";
            }
        } finally {
            //设置解锁key和解锁时间
            ResubmitLock.getInstance().unLock(lock, key, 10);
        }
    }

    @RequestMapping(value = "/getOpenBillListjz", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> getOpenBillListjz(String operationTime, String getTime, String reservationsId, String abbreviation, String areaAbbreviation) {
        return rechargeService.getOpenBillListjz(operationTime, getTime, reservationsId, abbreviation, areaAbbreviation);
    }

    @RequestMapping(value = "/getdcsByCustomerId", method = RequestMethod.POST)
    @ResponseBody
    public int getdcsByCustomerId(String id, String abbreviation) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return rechargeService.getdcsByCustomerId(id, abbreviation);
    }

    @RequestMapping(value = "/getOpenBillData", method = RequestMethod.POST)
    @ResponseBody
    public int getOpenBillData(String id, String abbreviation) {
        return rechargeService.getOpenBillData(id, abbreviation);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public int delete(String id, String abbreviation, String userAccount) {
        return rechargeService.deletes(id, abbreviation, userAccount);
    }

    /**
     * 修改时间  通过客户ID 进行修改
     */
    @RequestMapping(value = "/updateGetTime", method = RequestMethod.POST)
    @ResponseBody
    public int updateGetTimes(String reservationsId, String getTime, String updateTime, String abbreviation) {
        return rechargeService.updateGetTimes(reservationsId, getTime, updateTime,abbreviation);
    }

    //修改预约备注
    @RequestMapping(value = "/updateRechargeRemarksOk", method = RequestMethod.POST)
    @ResponseBody
    public int updateRechargeRemarksOk(String reservationsId, String rechargeTime, String remarks, String abbreviation) throws Exception {
        return rechargeService.updateRechargeRemarksOk(reservationsId, rechargeTime, remarks,abbreviation);
    }

    /**
     * 修改TC  通过客户ID 进行修改
     */
    @RequestMapping(value = "/updateTcName", method = RequestMethod.POST)
    @ResponseBody
    public int updateTcName(String customerId, String updateTcName,String abbreviation) {
        return rechargeService.updateTcName(customerId,  updateTcName, abbreviation);
    }

    @RequestMapping(value = "/updateDocumentNumber", method = RequestMethod.POST)
    @ResponseBody
    public int updateDocumentNumber(String getTime, String oldDocumentNumber, String documentNumber, String customerId,String abbreviation,String userAccount) {
        return rechargeService.updateDocumentNumber(getTime,  oldDocumentNumber,  documentNumber,  customerId, abbreviation, userAccount);
    }



}
  